import { constantRouterMap, router404 } from '@/router'
// import store from '@/store'
// import asyncRouterMap from '@/router/asyncRouter'

/**
 * 通过meta.role判断是否与当前用户权限匹配
 * @param roles
 * @param route
 */
// function hasPermission(roles, route) {
//   if (route.meta && route.meta.roles) {
//     return roles.some(role => route.meta.roles.includes(role))
//   } else {
//     return true
//   }
// }

/**
 * 递归过滤异步路由表，返回符合用户角色权限的路由表
 * @param routes asyncRouterMap
 * @param roles
 */
// function filterAsyncRouter(routes, roles) {
//   const res = []

//   routes.forEach(route => {
//     const tmp = { ...route }
//     if (hasPermission(roles, tmp)) {
//       if (tmp.children) {
//         tmp.children = filterAsyncRouter(tmp.children, roles)
//       }
//       res.push(tmp)
//     }
//   })

//   return res
// }

const permission = {
  state: {
    routers: constantRouterMap,
    addRouters: [],
    roleBtns: {}
  },
  mutations: {
    SET_ROUTERS: (state, routers) => {
      state.addRouters = routers
      state.routers = constantRouterMap.concat(routers)
    },
    ADD_ROLEBTNS: (state, btns) => {
      state.roleBtns = Object.assign({}, state.roleBtns, btns)
    }
  },
  actions: {
    GenerateRoutes({ commit }, data) {
      return new Promise(resolve => {
        const accessedRouters = []
        const routes = routingConfiguration(data)
        accessedRouters.push(...routes)
        accessedRouters.push(...router404)
        commit('SET_ROUTERS', accessedRouters)
        resolve()
      })
    },
    AddRoleBtns({ commit }, btns) {
      commit('ADD_ROLEBTNS', btns)
    }
  }
}

const _import = file => {
  if (process.env.NODE_ENV === 'production') return () => import('@/views/' + file)
  else return (resolve) => require(['@/views/' + file], resolve)
}

/**
 * 路由表挂import 并 配meta
 * @param routes
 */
function routingConfiguration(routes) {
  for (let i = 0; i < routes.length; i++) {
    routes[i].meta = {
      title: routes[i].sysName,
      icon: routes[i].icon,
      noCache: routes[i].noCache,
      alwaysShow: routes[i].alwaysShow,
      id: routes[i].id
    }
    // store.dispatch('AddRoleBtns', routes[i].button) // 配置按钮权限表
    const componentPath = routes[i].component
    routes[i].component = _import(routes[i].component) // 路由懒加载
    if (routes[i].children && Array.isArray(routes[i].children) && routes[i].children.length) {
      routes[i].children = routingConfiguration(routes[i].children)
    } else if ((routes[i].isMenu === '0' && routes[i].openType === '2') || ((routes[i].parentId == null || routes[i].hidden) && (!routes[i].children || !routes[i].children[0]))) {
      routes[i].alwaysShow = false
      if (routes[i].isMenu === '0' && routes[i].openType === '2') {
        routes[i].hidden = true
      }
      routes[i].component = _import('layout/Layout')
      routes[i].children = [{
        meta: routes[i].meta,
        path: '',
        name: routes[i].name,
        component: _import(componentPath.replace(/^\//, ''))
      }]

      delete routes[i].meta
      delete routes[i].name
    }
  }
  return routes
}

export default permission
